<?php
namespace app\api\controller;

class User extends Base
{

    /**
     * 发送验证码
     * @method send_code
     * @author chengzhigang
     * @param $phone
     * @
     */
    public function send_code()
    {
        try {
            $data = $this->param;
            //信息验证
            if (empty($data['phone'])) {
                apiReturn('手机号不能为空');
            }
            if (!check_phone($data['phone'])) {
                apiReturn('手机号格式不正确');
            }
            $code = rand(100000, 999999);
            $result = send_sms($data['phone'], $code);
            if ($result) {
                if ($result->Code == 'OK') {
                    //发送成功
                    $this->redis->setex('code_' . $data['phone'], Code_Time, $code);
                    apiReturn('发送成功', 0);
                } else {
                    //发送失败
                    throw new \Exception('发送失败：' . $result->Message);
                }
            } else {
                throw new \Exception('发送异常');
            }
        } catch (\Exception $e) {
            write_error_log($e); //记录错误日志
            apiReturn(Error_Log);
        }
    }

    /**
     * 用户注册
     * @method regist
     * @author chengzhigang
     * @param phone code password
     * @return
     */
    public function regist()
    {
        try {
            $data = $this->param;
            //注册验证
            if (empty($data['phone'])) {
                apiReturn('手机号不能为空');
            }
            if (!check_phone($data['phone'])) {
                apiReturn('手机号格式不正确');
            }
            if (empty($data['code'])) {
                apiReturn('验证码不能为空');
            }
            if ($data['code'] != $this->redis->get('code_' . $data['phone'])) {
                apiReturn('验证码不正确或已过时');
            } else {
                $this->redis->del('code_' . $data['phone']);
            }
            if (empty($data['password'])) {
                apiReturn('密码不能为空');
            }
            if (!check_password($data['password'])) {
                apiReturn('密码格式不正确');
            }
            $userinfo = $this->UserInfoModel->where('phone', $data['phone'])->find();
            if (!empty($userinfo)) {
                apiReturn('该手机号已注册');
            } else {
                $data['password'] = encryptPwd($data['password']);
                $res = $this->UserInfoModel->createData($data);
                if ($res) {
                    apiReturn('注册成功', 0);
                } else {
                    apiReturn('注册失败');
                }
            }
        } catch (\Exception $e) {
            write_error_log($e); //记录错误日志
            apiReturn(Error_Log);
        }
    }
    /**
     * 用户登录
     * @method login
     * @author chengzhigang
     * @param phone password
     * @return
     */
    public function login()
    {
        try {
            $data = $this->param;
            //登录验证
            if (empty($data['phone'])) {
                apiReturn('手机号不能为空');
            }
            if (!check_phone($data['phone'])) {
                apiReturn('手机号格式不正确');
            }
            if (empty($data['password'])) {
                apiReturn('密码不能为空');
            }
            if (!check_password($data['password'])) {
                apiReturn('密码格式不正确');
            }
            $userinfo = $this->UserInfoModel->where('phone', $data['phone'])->field('id,username,phone,nickname,head_pic,password,create_time')->find();
            if (empty($userinfo)) {
                apiReturn('该手机号未注册');
            }
            if (encryptPwd($data['password']) != $userinfo['password']) {
                apiReturn('密码错误');
            }
            //登录成功
            $token = buildRandStr(4);
            $this->redis->set('token_' . $userinfo['id'], $token);
            $userinfo['token'] = $token;
            unset($userinfo['password']);
            apiReturn('登录成功', 0, $userinfo);
        } catch (\Exception $e) {
            write_error_log($e); //记录错误日志
            apiReturn(Error_Log);
        }
    }

    /**
     * 更新用户信息
     * @method update_userinfo
     * @author chengzhigang
     * @param user_id username nickname
     * @return
     */
    public function update_userinfo()
    {
        try {
            $data = $this->param;
            //上传头像
            if (request()->file('head_pic')) {
                $result = upload(request()->file('head_pic'));
                if ($result['status'] == 0) {
                    $data['head_pic'] = $result['data']['path'];
                } else {
                    apiReturn($result['msg']);
                }
            } elseif (isset($data['head_pic'])) {
                unset($data['head_pic']);
            }
            //上传相册
            if (request()->file('album')) {
                $result = uploads(request()->file('album'));
                if ($result['status'] == 0) {
                    $albums = array_column($result['data'], 'path');
                    $data['album'] = implode(',', $albums);
                } else {
                    apiReturn($result['msg']);
                }
            } elseif (isset($data['album'])) {
                unset($data['album']);
            }
            $res = $this->UserInfoModel->updateData(array('id' => $data['user_id']), $data);
            if ($res) {
                $userinfo = $this->UserInfoModel->where('id', $data['user_id'])->field('id,username,phone,nickname,head_pic,password,create_time')->find();
                apiReturn('更新成功', 0, $userinfo);
            } else {
                apiReturn('更新失败');
            }
        } catch (\Exception $e) {
            write_error_log($e); //记录错误日志
            apiReturn(Error_Log);
        }
    }

    /**
     * 获取用户信息
     * @method get_userinfo
     * @author chengzhigang
     * @param user_id token
     * @return
     */
    public function get_userinfo()
    {
        try {
            $data = $this->param;
            $userinfo = $this->UserInfoModel->where('id', $data['user_id'])->field('id,username,nickname,phone,head_pic,album,create_time')->find();
            if (empty($userinfo)) {
                apiReturn('用户不存在');
            } else {
                apiReturn('请求成功', 0, $userinfo);
            }
        } catch (\Exception $e) {
            write_error_log($e); //记录错误日志
            apiReturn(Error_Log);
        }
    }

    /**
     * 修改密码
     * @method change_pwd
     * @author chengzhigang
     * @param old_pwd user_id new_pwd confirm_old
     * @return
     */
    public function change_pwd()
    {
        try {
            $data = $this->param;
            if (!isset($data['old_pwd']) || empty($data['old_pwd'])) {
                apiReturn('请输入旧密码');
            }
            if (!isset($data['new_pwd']) || empty($data['new_pwd'])) {
                apiReturn('请输入新密码');
            }
            if (!check_password($data['new_pwd'])) {
                apiReturn('新密码格式错误');
            }
            if (!isset($data['confirm_pwd']) || empty($data['confirm_pwd'])) {
                apiReturn('请输入确认密码');
            }
            if ($data['old_pwd'] == $data['new_pwd']) {
                apiReturn('新密码与旧密码不能一致');
            }
            if ($data['confirm_pwd'] != $data['new_pwd']) {
                apiReturn('确认密码错误');
            }
            $password = $this->UserInfoModel->where('id', $data['user_id'])->value('password');
            if (empty($password)) {
                apiReturn('用户不存在');
            }
            if ($password != encryptPwd($data['old_pwd'])) {
                apiReturn('旧密码错误');
            }
            $res = $this->UserInfoModel->updateData(array('id' => $data['user_id']), array('password' => encryptPwd($data['new_pwd'])));
            if ($res) {
                apiReturn('修改成功', 0);
            } else {
                apiReturn('修改失败');
            }
        } catch (\Exception $e) {
            write_error_log($e); //记录错误日志
            apiReturn(Error_Log);
        }
    }

    /**
     * 忘记密码
     * @method forget_pwd
     * @author chengzhigang
     * @param phone password code
     * @return
     */
    public function forget_pwd()
    {
        try {
            $data = $this->param;
            if (!isset($data['phone']) || empty($data['phone'])) {
                apiReturn('请输入手机号');
            }
            if (!check_phone($data['phone'])) {
                apiReturn('手机号格式不正确');
            }
            if (!isset($data['code']) || empty($data['code'])) {
                apiReturn('请输入验证码');
            }
            if ($data['code'] != $this->redis->get('code_' . $data['phone'])) {
                apiReturn('验证码不正确或已过时');
            } else {
                $this->redis->del('code_' . $data['phone']);
            }
            if (!isset($data['password']) || empty($data['password'])) {
                apiReturn('请输入密码');
            }
            if (!check_password($data['new_pwd'])) {
                apiReturn('密码格式错误');
            }
            $res = $this->UserInfoModel->updateData(array('phone' => $data['phone']), array('password' => encryptPwd($data['password'])));
            if ($res) {
                apiReturn('操作成功', 0);
            } else {
                apiReturn('操作失败');
            }
        } catch (\Exception $e) {
            write_error_log($e); //记录错误日志
            apiReturn(Error_Log);
        }
    }
}
